#include "Solver.h"

Solver::Solver(Node *root) {
	node = root;
	countChildren = 0;
	countNode = 0;
}

void Solver::createRootStandard() {
	std::cin >> rowNum;
	std::cin >> colNum;
	std::vector<std::vector<int>*> *inState = new std::vector<std::vector<int>*>();

	for (int x = 0; x < rowNum; x++) {
		std::vector<int> *tempVector = new std::vector<int>;
		
		for (int j = 0; j < colNum; j++) {
			int tempInt;
			std::cin >> tempInt;
			tempVector->push_back(tempInt);
		}
		inState->push_back(tempVector);
	}
	node = new Node(NULL, inState);
}

bool Solver::checkWinState() {
	int i = 1;
	bool flag = true;
	int max = 0;
	
	std::vector<std::vector<int>*> *state = node->getState();
	std::vector<std::vector<int>*>::iterator it;

	for (it = state->begin(); it != state->end(); ++it)	{
		if (max == 0) {
			max = state->size()*(*it)->size();
		}

		std::vector<int>::iterator it2;

		for (it2 = (*it)->begin(); it2 != (*it)->end(); ++it2) {
			if (i%max != *it2) {
				flag = false;
				break;
			}
			i++;
		}

		if (!flag) {
			break;
		}
	}
	return flag;
}

bool Solver::compare(Node *node1, Node *node2) {
	std::vector<std::vector<int>*> *state1 = node1->getState();
	std::vector<std::vector<int>*> *state2 = node2->getState();
	bool flag = true;

	for (int i = 0; i < state1->size(); i++) {
		for (int j = 0; j < state1->at(i)->size(); j++) {
			if (state1->at(i)->at(j) != state2->at(i)->at(j)) {
				flag = false;
				break;
			}
		}
		
		if (!flag) {
			break;
		}
	}
	return flag;
}

std::string Solver::findPath(Node *node) {
	if (node->getParent() == NULL) {
		return "" + node->getDirection();
	}
	return findPath(node->getParent()) + node->getDirection();
}

int Solver::getCountNode() {
	return countNode;
}

int Solver::getCountChildren() {
	return countChildren;
}

int Solver::getMaxDepth() {
	return maxDepth;
}


Node* Solver::getActualNode(){
	return node;
}

void Solver::randomOrder(std::string *order){
	std::random_shuffle(order->begin(), order->end());
}



Solver::~Solver()
{
}
